package fun.ipconfig.shopping.controller;


import fun.ipconfig.shopping.entity.Result;
import fun.ipconfig.shopping.entity.UserEntity;
import fun.ipconfig.shopping.service.UserService;
import jakarta.annotation.security.DenyAll;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextHolderStrategy;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.security.web.context.SecurityContextRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gtl
 */
@Controller
public class UserController {

    Logger logger = LoggerFactory.getLogger(UserController.class);
    private final UserService userService;
    private final AuthenticationConfiguration authenticationConfiguration;
    private final SecurityContextRepository securityContextRepository = new HttpSessionSecurityContextRepository();

    public UserController(UserService userService, AuthenticationManager authenticationManager, AuthenticationConfiguration authenticationConfiguration ){
        this.userService = userService;
        this.authenticationConfiguration = authenticationConfiguration;

    }

    
    @GetMapping("/urls")
    @ResponseBody
    public List<String> getUrls(){
        SecurityContextHolder.getContext().getAuthentication();
        System.out.println(Thread.currentThread());
        return userService.getAllUrl();
    }

    @PostMapping("/success")
    @ResponseBody
    public Result loginSuccess(){
        return new Result().setStatus(200).setMsg("登录成功");
    }

    @RequestMapping("/fail/{msg}")
    @ResponseBody
    public Result loginFail(@PathVariable("msg") String msg){
        return new Result().setStatus(403).setMsg("登录失败!"+msg);
    }
}
